package com.cypher.encryptionanddecryption.fragment.protocol.nb25;

import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.cypher.encryptionanddecryption.R;
import com.cypher.encryptionanddecryption.activity.BluetoothCommunicationActivity;
import com.cypher.encryptionanddecryption.bean.CommunicationBean;
import com.cypher.encryptionanddecryption.fragment.protocol.AbstractProtocolFragment;
import com.cypher.encryptionanddecryption.util.HexUtils;
import com.cypher.encryptionanddecryption.util.Nb25ProtocolUtils;
import com.cypher.encryptionanddecryption.watcher.DecimalDigitsWatcher;

import org.apache.commons.lang3.StringUtils;

/**
 * 异常大流量参数集页面
 * @author: 古霸天
 * @date: 2020/12/14
 */
public class AbnormallyHighFlowParamsFragment extends AbstractProtocolFragment {

    private final String[] energyArr = {"禁止", "使能"};


    /**
     * 天数/月份
     */
    private Spinner energySpinner;
    /**
     * 周期值输入框
     */
    private EditText restrictTxt;
    private TextView energySpinnerTitle;
    private TextView restrictTxtTitle;

    private CommunicationBean bean;
    private BluetoothCommunicationActivity activity;
    public AbnormallyHighFlowParamsFragment(BluetoothCommunicationActivity activity, CommunicationBean bean) {
        this.activity = activity;
        this.bean = bean;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_nb25_abnormally_high_flow_params, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
    }

    /**
     * 初始化
     * @param v
     */
    private void init(View v) {
        energySpinnerTitle = v.findViewById(R.id.abnormally_high_flow_energy_title);
        restrictTxtTitle = v.findViewById(R.id.abnormally_high_flow_restrict_title);

        ArrayAdapter adapter = new ArrayAdapter<String>(v.getContext(), R.layout.spinner_item, energyArr);
        adapter.setDropDownViewResource(R.layout.dropdown_stytle);
        energySpinner = v.findViewById(R.id.abnormally_high_flow_energy);
        energySpinner.setAdapter(adapter);

        // 定义输入框过滤器
        InputFilter[] inputFilters = new InputFilter[1];
        // 设置输入框输入内容长度
        inputFilters[0] = new InputFilter.LengthFilter(4);

        restrictTxt = v.findViewById(R.id.abnormally_high_flow_restrict);
        // 设置小数点后位数限制
        restrictTxt.addTextChangedListener(new DecimalDigitsWatcher(restrictTxt, 2));
        restrictTxt.setFilters(inputFilters);

        switch (bean.getHexCode()) {
            case "2023":
                energySpinnerTitle.setText("异常大流量报警使能：");
                restrictTxtTitle.setText("异常大流量门限：");
                restrictTxt.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                break;
            case "2202":
                energySpinnerTitle.setText("微小流泄露报警使能：");
                restrictTxtTitle.setText("微小流报警门限：");
                restrictTxt.setInputType(InputType.TYPE_CLASS_NUMBER);
                break;
        }

    }

    @Override
    public void fillFragment(String data) {
        reader(Nb25ProtocolUtils.formatReceiveData(data));
    }
    @Override
    public String getHexCode(String rwType) {
        if (StringUtils.equals("read", rwType)) {
            return Nb25ProtocolUtils.getReadCommand(bean.getHexCode(), PROTOCOL_VERSION);
        }
        // 组包
        return getData();
    }

    /**
     * 渲染页面
     * @param data
     */
    private void reader(String data) {
        energySpinner.setSelection(StringUtils.equals("00", data.substring(0, 2)) ? 0 : 1);
        switch (bean.getHexCode()) {
            case "2023":
                restrictTxt.setText(HexUtils.formatDouble(
                        HexUtils.integerToDouble(Integer.parseInt(data.substring(2, 6), 16), 2), 2));
                break;
            case "2202":
                restrictTxt.setText(String.valueOf(Integer.parseInt(data.substring(2, 6), 16)));
                break;
        }

    }
    /**
     * 获取数据域
     * @return
     */
    private String getData() {

        StringBuffer data = new StringBuffer();

        String restrict = restrictTxt.getText().toString();
        if (StringUtils.isEmpty(restrict)) {
            Toast.makeText(activity, "异常大流量门限不能为空", Toast.LENGTH_SHORT).show();
            return data.toString();
        }

        data.append((energySpinner.getSelectedItemPosition() == 0 ? "00" : "01"));

        switch (bean.getHexCode()) {
            case "2023":
                data.append(HexUtils.doubleToHex(Double.parseDouble(restrict), 2, 4));
                break;
            case "2202":
                data.append(HexUtils.paddingLeft(Integer.toHexString(Integer.parseInt(restrict)), 4));
                break;
        }
        return Nb25ProtocolUtils.getWriteCommand(bean.getHexCode(), data.toString(), PROTOCOL_VERSION);
    }
}
